package com.easy.tool.utils.decimal;

import com.easy.tool.anno.BeanDes;
import com.easy.tool.anno.MethodDes;
import com.easy.tool.entity.RuleEntity;

import java.math.BigDecimal;

/**
 * @author 高俊
 * @create 2021-11-2021/11/10-16:26
 */
@BeanDes(name = "BigDecimalUtils运算",des = "BigDecimal类型链式运算")
public class BigDecimalUtils {

    private BigDecimal result = BigDecimal.ZERO;

    public BigDecimalUtils() {
    }


    public BigDecimal getResult() {
        return result;
    }

    public void setResult(BigDecimal result) {
        this.result = result;
    }

    /**
     *
     * @param val
     * @param <T>
     * @return
     */
    @MethodDes(des = "创建BigDecimal",param = {"arg0 -> val：初始入参"})
    public static <T> BigDecimalUtils createDecimal(T val){
        BigDecimalUtils utils = new BigDecimalUtils();

        BigDecimal decimal = new BigDecimal(val.toString());

        utils.setResult(decimal);
        return utils;
    }


    /**
     * 加法
     * @param val
     * @param <T>
     * @return
     */
    @MethodDes(des = "BigDecimal加法",param = {"arg0 -> val：操作参数"})
    public <T> BigDecimalUtils add(T val){
        BigDecimal add = this.getResult().add(new BigDecimal(val.toString()));
        this.setResult(add);
        return this;
    }

    /**
     * 减法
     * @param val
     * @param <T>
     * @return
     */
    @MethodDes(des = "BigDecimal减法",param = {"arg0 -> val：操作参数"})
    public <T> BigDecimalUtils subtract(T val){
        BigDecimal decimal = this.result.subtract(new BigDecimal(val.toString()));
        this.setResult(decimal);
        return this;
    }

    /**
     * 乘法
     * @param val
     * @param <T>
     * @return
     */
    @MethodDes(des = "BigDecimal除法",param = {"arg0 -> val：操作参数"})
    public <T> BigDecimalUtils multiply(T val){
        BigDecimal decimal = this.result.multiply(new BigDecimal(val.toString()));
        this.setResult(decimal);
        return this;
    }

    /**
     * 除法
     * @param val
     * @param <T>
     * @return
     */
    @MethodDes(des = "BigDecimal除法",param = {"arg0 -> val：操作参数"})
    public <T> BigDecimalUtils divide(T val){
        BigDecimal decimal = this.result.divide(new BigDecimal(val.toString()));
        this.setResult(decimal);
        return this;
    }

}
